package module.map.utils
{
	import drongo.desktop.utils.UtilsFile;
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.values.CollectionValue;

	import flash.filesystem.File;

	import module.map.model.MapModel;
	import module.map.model.NodeTypes;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;

	public class ExportUtils
	{
		public function ExportUtils()
		{

		}

		/**
		 * 编码地图导出
		 */
		public static function ExportMap(file:File, project:ProjectModel, mirrorY:Boolean):void
		{
			var map:MapModel = project.currentMap;
			var result:Object = {};
			// 地图宽高
			result.width = map.Get(Propertys.WIDTH).GetValue();
			result.height = map.Get(Propertys.HEIGTH).GetValue();
			// 寻路网格大小
			result.navWidth = map.Get(Propertys.NAV_WIDTH).GetValue();
			result.navHeight = map.Get(Propertys.NAV_HEIGTH).GetValue();

			var nodes:ArrayProperty = map.Get(Propertys.NODES) as ArrayProperty;
			var node:CollectionValue;

			// 塔点
			var towers:Array = [];
			// 路径列表
			var paths:Object = {};
			// 图片
			var images:Array = [];
			// 区域
			var areas:Array = [];
			var x:Number, y:Number;
			var name:String;
			var type:Number;
			for (var i:int = 0; i < nodes.length; i++)
			{
				node = nodes.GetAt(i) as CollectionValue;
				type = node.Get(Propertys.TYPE).GetValue();
				switch (type)
				{
					case NodeTypes.PATH:
					{
						name = node.Get(Propertys.NAME).GetValue();
						var count:Number = node.Get(Propertys.COUNT).GetValue();
						var list:Array = [];
						PathUtils.GetPath2(node, result.navWidth, result.navHeight, list, count, mirrorY);
						var len:int = list.length;
						for (var index:int = 0; index < len; index++)
						{
							paths[name + "_" + index] = list[index];
						}
						break;
					}
					case NodeTypes.TOWER: // 塔点
					{
						x = node.Get(Propertys.X).GetValue();
						if (mirrorY)
						{
							y = 0 - node.Get(Propertys.Y).GetValue();
						}
						else
						{
							y = node.Get(Propertys.Y).GetValue();
						}
						towers.push(
								{
									id: node.Get(Propertys.ID).GetValue(),
									x: x,
									y: y,
									type: node.Get(Propertys.TOWER_TYPE).GetValue()
								});
						break;
					}
					case NodeTypes.IMAGE:
					{
						images.push(
								{
									id: node.Get(Propertys.ID).GetValue(),
									x: node.Get(Propertys.X).GetValue(),
									y: node.Get(Propertys.Y).GetValue(),
									image: node.Get(Propertys.PATH).GetValue()
								});
						break;
					}
					case NodeTypes.AREA:
					{
						name = node.Get(Propertys.NAME).GetValue();
						var point:CollectionValue;
						x = result.navWidth + node.Get(Propertys.X).GetValue();
						y = result.navHeight + node.Get(Propertys.Y).GetValue();
						var points:ArrayProperty = node.Get(Propertys.POINTS) as ArrayProperty;
						var area:Object = {};
						area.name = name;
						area.type = node.Get(Propertys.AREA_TYPE).GetValue();
						area.points = [];
						var oldPoint:CollectionValue;
						for (var idx:Number = 0; idx < points.length; idx++)
						{
							point = points.GetAt(idx) as CollectionValue;
							if (idx > 1)
							{
								if (mirrorY)
								{
									area.points.push(
											x + oldPoint.Get(Propertys.X).GetValue(),
											0 - (y + oldPoint.Get(Propertys.Y).GetValue())
										);
								}
								else
								{
									area.points.push(
											x + oldPoint.Get(Propertys.X).GetValue(),
											y + oldPoint.Get(Propertys.Y).GetValue()
										);
								}
							}
							if (mirrorY)
							{
								area.points.push(
										x + point.Get(Propertys.X).GetValue(),
										0 - (y + point.Get(Propertys.Y).GetValue())
									);
							}
							else
							{
								area.points.push(
										x + point.Get(Propertys.X).GetValue(),
										y + point.Get(Propertys.Y).GetValue()
									);
							}
							oldPoint = point;
						}
						// 形成闭环
						area.points.push(area.points[area.points.length - 2], area.points[area.points.length - 1]);
						area.points.push(area.points[0], area.points[1]);
						areas.push(area);
						break;
					}
					default:
					{
						break;
					}
				}
			}

			result.paths = paths;
			result.towers = towers;
			result.images = images;
			result.areas = areas;

			// 导出Json
			var jsonStr:String = JSON.stringify(result, null, " ");
			UtilsFile.saveJson(file, jsonStr);

			// image
			var imageDir:File = new File(file.parent.nativePath + File.separator + "images" + File.separator);
			if (imageDir.exists == null)
			{
				imageDir.createDirectory();
			}
			var imageData:Object;
			var srcFile:File;
			var targetFile:File;
			for (var j:int = 0; j < images.length; j++)
			{
				imageData = images[j];
				srcFile = new File(project.imagesFile.nativePath + File.separator + imageData.image);
				targetFile = new File(imageDir.nativePath + File.separator + imageData.image);
				UtilsFile.copyFile(srcFile, targetFile);
			}
		}
	}
}